swController.onUpdate = updateUI;
swController.onError  = handleError;
swClock.onTick = function () { }
swClock.onChange = function () { }

if (!window.queryObj) {
    window.queryObj = new Object();
    window.location.search.replace(new RegExp("([^?=&]+)(=([^&]*))?","g"), function($0,$1,$2,$3) { queryObj[$1] = $3; });
}

swController.queryObj = window.queryObj;

function ie_getElementsByTagName(str) {
  if (str=="*") return document.all;
  else return document.all.tags(str);
}

if (document.all) 
  document.getElementsByTagName = ie_getElementsByTagName;

    
function updateUI ( cmd ) {
	clearTimeout ( delayTimer );

	if ( swController.redir ) {
		if (swController.redir.originalURL != null &&
		    swController.redir.originalURL != '') {
		    setElementValue('originalURL', '<a target="_blank" href="' + swController.redir.originalURL +
			    '">' + swController.redir.originalURL + '</a>',true);
		}

    	}


	if ( swController.clientState == 1 ) {
		hidePage('swclick');
		hidePage('waitPage-click');

		hidePage('swFB');
		hidePage('waitPage-FB');

		hidePage('swTWT');
		hidePage('waitPage-twt');

		hidePage('sw-header');
		showPage('statusPage');

	} else {
		showPage('sw-header');
		hidePage('statusPage');

		showPage('swclick');

		showPage('swFB');
		showPage('swTWT');
	}
	if ( swController.clientState == -1 ) {
		$('#btn-connect-click').prop('disabled', true);
		$('#btn-connect-fb').prop('disabled', true);
		$('#btn-connect-twt').addClass("disabled");
		showPage('sw-status-wait');
	}else{
		hidePage('sw-status-wait');
	}
	if ( swController.clientState == 0 ) {
		$('#btn-connect-click').prop('disabled', false);
		$('#btn-connect-fb').prop('disabled', false);
		$('#btn-connect-twt').removeClass("disabled");
	}

}

swClock.onChange = function ( newval ) {
    setElementValue("sessionTime", swController.formatTime(newval));
}

function handleError( code ) {
	//var e = document.getElementById("sw-error");
	setElementValue("sw-error", "Error: " + code);
	hidePage('sw-status-wait');
    clearTimeout(delayTimer);
    //showErrorPage(code);
//	alert(code);
	
}

function connect() {
    var username =  'username' ; 
    var password =  'password';
    //post_to_wall();
    //showWaitPage(1000);
	//showPage('waitPage-click');
    //swController.logon( username , password ) ;
}


function disconnect() {
    if (confirm("Are you sure you want to disconnect now?")) {
	hidePage('waitPage-twt');
	swClock.stop();
	swController.logoff();
	FB.getLoginStatus(updateButton, true);
    }
    return false;
}

function setElementValue(elem, val, forceHTML) {
    var e = document.getElementById(elem);
    if (e != null) {
	var node = e;
	if (!forceHTML && node.firstChild) {
	    node = node.firstChild;
	    node.nodeValue = val;
	} else {
	    node.innerHTML = val;
	}
    }
}
function hidePage(page) { 
    var e = document.getElementById(page);
    if (e != null) e.style.display='none';
}

function showPage(page) { 
    var e = document.getElementById(page);
    if (e != null) e.style.display='inline';
}

function showWaitPage(delay) {
    /* Wait for delay  */
    clearTimeout(delayTimer);	
    if (typeof(delay) == 'number' && (delay > 10)) {
		delayTimer= setTimeout('showWaitPage(0)' , delay);
		return;
    }
    
    /* show the waitPage */
    //hidePage("logonPage");

}

function swConnect(type) {
	if (swController.clientState == -1) {
		return false;
	}
	var username;
	var password;
	if (type == 'click') {
		var u = document.getElementById('click-username');
		var p = document.getElementById('click-password');
		if (u != null) {username = u.value} else {username ='';}
		if (p != null) {password = p.value} else {password ='';}

		if (username != '' && password != '') {
			showPage('waitPage-click');		 
			setTimeout('swController.logon(\''+ username + '\',\'' + password + '\')', 3000);
		} else {
			alert("Username/Password is not set");
		}
	}

	if (type == 'facebook') {
		var u = document.getElementById('fb-username');
		var p = document.getElementById('fb-password');
		if (u != null) {username = u.value} else {username = '';}
		if (p != null) {password = p.value} else {password = '';}

		if (username != '' && password != '') {
			showPage('waitPage-FB');		 
			setTimeout('swController.logon(\''+ username + '\',\'' + password + '\')', 3000);
			post_to_wall();

		} else {
			alert("Username/Password is not set");
		}
	}
	
	if (type == 'twitter') {
		var u = document.getElementById('twt-username');
		var p = document.getElementById('twt-password');
		if (u != null) {username = u.value} else {username ='';}
		if (p != null) {password = p.value} else {password ='';}

		if (username != '' && password != '') {
			showPage('waitPage-twt');		 
			setTimeout('swController.logon(\''+ username + '\',\'' + password + '\')', 3000);
		} else {
			alert("Username/Password is not set");
		}
	}
	setTimeout('window.scrollTo(0,0)', 3000);
		
}

var swjsWindowOnLoad = window.onload;
var delayTimer; // global reference to delayTimer

window.onload = function() {
//    if (swjsWindowOnLoad) 
//	swjsWindowOnLoad();
	
	//hidePage('logon-click');
    //showWaitPage();

    setTimeout('swController.refresh()', 500);


}

